package pers.cz.service.impl;

import pers.cz.business.TestCaseBusiness;
import pers.cz.model.TestCase;
import pers.cz.model.TestCaseParams;
import pers.cz.piny.PinyUtil;
import pers.cz.pub.BeanHelper;
import pers.cz.service.TestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: QueryTestCaseServiceImpl
 * @author: Cheng Zhi
 * @create: 2021-03-12 16:53
 **/
@Service
public class TestCaseServiceImpl implements TestCaseService {

    @Autowired
    BeanHelper beanHelper;

    @Override
    public List<TestCase> getAllTestcase() {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCase();
        return testCaseList;
    }

    @Override
    public List<TestCase> getAllTestcase(String busiCode) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCase(busiCode);
        return testCaseList;
    }

    @Override
    public List<TestCase> getAllTestcaseByPage(Integer pageStart, Integer pageLimit, Integer userId) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCaseByPage(pageStart, pageLimit, userId);
        return testCaseList;
    }

    @Override
    public TestCase queryTestCaseById(Integer id) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        TestCase testCase = business.queryTestCaseById(id);
        return testCase;
    }

    @Override
    public List<TestCase> queryAllTestCaseIds(List<Integer> ids) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCaseIds(ids);
        return testCaseList;
    }

    @Override
    public Integer updateTestCase(TestCase testCase) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        int count = business.updateTestCase(testCase);
        return count;
    }

    @Override
    public Integer addTestCase(Integer caseId, String caseName, String request, String expectedResponse, String busiCode, String ignoreFields,String caseNameZh, Integer userId) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        int count = business.addTestCase(caseId, caseName, request,expectedResponse,busiCode,ignoreFields,caseNameZh,userId);
        return count;
    }

    @Override
    public Integer deleteTestCase(Integer id) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        int count = business.deleteTestCase(id);
        return count;
    }

    @Override
    public Integer getCountTestCase(Integer userId) {

        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        int count = business.getCountTestCase(userId);
        return count;
    }

    @Override
    public Map<Integer, List<String>> selectAllIdAndCaseName() {

        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCase();
        Map<Integer, List<String>> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap();
        for (TestCase testCase : testCaseList) {
            List<String> list = new ArrayList<>();
            try {
                String caseName = testCase.getCaseName();
                list.add(caseName);
                list.add(PinyUtil.getPingYin(caseName));
                map.put(testCase.getId(),list);
            } catch (Exception e) {

            }
        }

        return map;
    }

    @Override
    public List<TestCase> getAllTestCaseByTestName(String testName) {

        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        List<TestCase> testCaseList = business.queryAllTestCaseByTestName(testName);
        return testCaseList;
    }

    @Override
    public List<TestCase> getAllTestCaseByFuzzy(String testCaseName) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.queryTestCaseByFuzzy(testCaseName);
    }

    @Override
    public List<TestCaseParams> queryAllTestCaseParams(Integer pageStart, Integer pageLimit) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.queryAllTestCaseParamsByPage(pageStart, pageLimit);
    }

    @Override
    public Integer getCountTestCaseParam() {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.getCountTestCaseParams();
    }

    @Override
    @CacheEvict(value={"TestCaseParams"},allEntries = true) // 自动更新缓存
    public Integer addTestCaseParam(TestCaseParams testCaseParams) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.addTestCaseParams(testCaseParams);
    }

    @Override
    @CacheEvict(value={"TestCaseParams"},allEntries = true)
    public Integer updateTestCaseParam(TestCaseParams testCaseParams) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.updateTestCaseParams(testCaseParams);
    }

    @Override
    @CacheEvict(value={"TestCaseParams"},allEntries = true)
    public Integer deleteTestCaseParam(Integer id) {
        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.deleteTestCaseParams(id);
    }

    @Override
    public TestCaseParams queryTestCaseParamsById(Integer id) {

        TestCaseBusiness business = new TestCaseBusiness();
        business.setMapper(beanHelper);
        return business.queryTestCaseParamsById(id);
    }


}
